package co.edu.unal.satacredito.queue;

import co.edu.unal.satacredito.database.connection.TransactionDatastoreFactory;
import co.edu.unal.satacredito.database.constants.TransactionConstants;
import co.edu.unal.satacredito.database.dao.TransactionMongoDao;
import co.edu.unal.satacredito.database.entity.Transaction;
import co.edu.unal.satacredito.ejbs.ValidationEJB;
import co.edu.unal.satacredito.queue.entity.TransactionPojo;
import com.google.code.morphia.Datastore;
import java.util.Date;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.xml.ws.WebServiceRef;
import org.bson.types.ObjectId;

@MessageDriven(mappedName = "jms/Queue")
public class QueueReader implements MessageListener {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/EventLogWebService.WSDL.wsdl")
    private webservice.EventLogWebService_Service service;

    @Resource
    private MessageDrivenContext mdc;
    private static final Logger logger = null;

    @Override
    public void onMessage(Message msg) { 
        
        //MapMessage mmsg = null;
        ObjectMessage omsg = null;

        try {
            //mmsg = (MapMessage) msg;
            omsg = (ObjectMessage) msg;
            
            Datastore datastore = TransactionDatastoreFactory.createDatastore();
            Transaction transactionOut = new Transaction();

            TransactionPojo transactionIn = (TransactionPojo) omsg.getObject();
            
            transactionOut.setFileId(new ObjectId(transactionIn.getFileId()));
            transactionOut.setFileNumber(transactionIn.getFileNumber());
            transactionOut.setName(transactionIn.getName());
            transactionOut.setNin(transactionIn.getNin());
            transactionOut.setNinType(transactionIn.getNinType());
            transactionOut.setCardCode(transactionIn.getCardCode());
            transactionOut.setCardNumber(transactionIn.getCardNumber());
            transactionOut.setCardType(transactionIn.getCardType());
            transactionOut.setCardExpirationDate(transactionIn.getCardExpirationDate());
            transactionOut.setBank(new ObjectId(transactionIn.getBank()));
            transactionOut.setFranchise(new ObjectId(transactionIn.getFranchise()));
            transactionOut.setPurchaseDate(new Date(transactionIn.getPurchaseDate()));
            transactionOut.setPurchaseAmount(transactionIn.getPurchaseAmount());
            transactionOut.setPurchaseDetail(transactionIn.getPurchaseDetail());          
            
            transactionOut.setValidationDate(null);
            transactionOut.setValidationResult(TransactionConstants.RESULT_NOT);
            transactionOut.setValidationIssue(TransactionConstants.ISSUE_NOT);
            transactionOut.setReadState(TransactionConstants.READ_SYNTAXIS);
            /*
             * 
             * Envio TransactionOut A WS ->
             
             */
            System.out.println("WSReception: "+transactionIn);
            TransactionMongoDao tmd = new TransactionMongoDao(datastore);
            
            ValidationEJB vejb = new ValidationEJB();
            
            String Token = startProcess(vejb.getMachineName(), transactionOut.getFileId().toString(), 1);
            tmd.create(transactionOut);
            finalizeProcess(Token);
             System.out.println("QueueReader: Received from Queue");

        } catch (Exception e) {
            e.printStackTrace();
            mdc.setRollbackOnly();
        } catch (Throwable th) {
            th.printStackTrace();            
        }
    }

    public void ejbRemove() throws EJBException {
        logger.fine("ejbRemove()");
    }

    private String startProcess(java.lang.String machineName, java.lang.String fileCheck, int typeOfWork) throws webservice.PreexistingEntityException_Exception, webservice.NonexistentEntityException_Exception {
        webservice.EventLogWebService port = service.getEventLogWebServicePort();
        return port.startProcess(machineName, fileCheck, typeOfWork);
    }

    private String finalizeProcess(java.lang.String token) {
        webservice.EventLogWebService port = service.getEventLogWebServicePort();
        return port.finalizeProcess(token);
    }
}